package com.hmall.item.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.FiltersDto;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.pojo.Filters;
import com.hmall.common.pojo.Item;
import com.hmall.item.service.IItemService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO getPage(@Param("page") Integer page, @Param("size") Integer size) {
        Page<Item> itemPage = new Page<>(page, size);
        IPage<Item> page1 = itemService.page(itemPage);
        return new PageDTO(page1.getTotal(), page1.getRecords());
    }

    /**
     * 根据Id查询商品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id) {
        Item item = itemService.getById(id);
        return item;
    }

    /**
     * 添加商品
     *
     * @param item
     */
    @PostMapping
    public void insert(@RequestBody Item item) {
        item.setCreateTime(LocalDateTime.now());
        item.setUpdateTime(LocalDateTime.now());
        itemService.save(item);
    }

    /**
     * 修改商品状态
     *
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void update(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        itemService.updateStatusById(id, status);
    }

    /**
     * 根据Id修改
     *
     * @param item
     */
    @PutMapping
    public void update(@RequestBody Item item) {
        item.setUpdateTime(LocalDateTime.now());
        itemService.updateById(item);
    }

    /**
     * 根据Id删除
     *
     * @param id
     */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        itemService.removeById(id);
    }


    //searchservice 查询商品过滤条件
    @PostMapping("/selectfilter")
    public Filters selectFilters(@RequestBody FiltersDto filtersDto) {
        return itemService.getFilters(filtersDto);
    }

    //searchservice 根据条件查询商品
    @PostMapping("/list")
    public PageDTO<Item> searchByCondition(@RequestBody FiltersDto filtersDto) {
        PageDTO<Item> items = itemService.searchByCondition(filtersDto);
        return items;
    }

    //扣减库存
    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable("itemId") Long id, @PathVariable("num") Integer num) {
        itemService.updateStock(id, num);
    }

    //恢复库存
    @PutMapping("restock/{itemId}/{num}")
    void rebackStock(@PathVariable("itemId") Long id, @PathVariable("num") Integer num) {
        itemService.reStock(id, num);
    }


    //查询热点数据
    @GetMapping("/hot")
    List<Item> hotItems() {
     return itemService.hotItems();
    }
}
